# CRMEB：
# 1、用户：查询用户分组——>添加用户分组——>修改分组——>删除分组
# 2、客户：添加话术——>修改话术——>删除话术
# 3、用户：添加标签——>删除标签
base_url = "http://47.99.66.20:8899"
import requests
import pytest
import allure
from faker import Faker
from databases import DB
faker = Faker(locale='zh_cn')
name = faker.name()
id_list = []
@allure.feature("CRMEB")
class TestCrmeb:
    @allure.story("登录前置")
    @pytest.fixture()
    def login(self):
        # 发起请求
        response = requests.post(base_url+"/adminapi/login",json={"account":"admin","pwd":"123456","key":"68c8c070d3856","captchaType":"blockPuzzle","captchaVerification":""})
        # 断言
        assert response.status_code == 200
        assert response.json()['msg'] =='success'
        yield response.json()['data']['token']
    @allure.story("新增用户分组")
    def test_addUserGroup(self,login):
        response = requests.post(base_url+"/adminapi/user/user_group/save.html",json={"group_name":str(name)},headers={"Authori-zation":"Bearer " + str(login)})
        # 断言
        assert response.status_code == 200
        assert response.json()['status'] == 200
        assert response.json()['msg'] =='提交成功'
        assert response.json()['code'] == '100017'
    @allure.story("修改用户分组")
    def test_updateUserGroup(self,login):
        """
        # 先去查询，拿到id
        response = requests.get(base_url+"/adminapi/user/user_group/list?page=1&limit=15",headers={"Authori-zation":"Bearer " + str(login)})
        # 判断拿到id
        for i in response.json()['data']['list']:
            if i['group_name'] == str(name):
                id_list.append(i['id'])     #把要操作的id加到列表里面去
        """
        d1 = DB('root', '1965331442Xby@', 'crmeb', '47.99.66.20')
        result = d1.searchAll(f'select * from eb_user_group where group_name="{name}"')
        id = result[0]['id']
        id_list.append(id)
        # 修改分组
        response = requests.post(base_url+"/adminapi/user/user_group/save.html",json={"id":id_list[0],"group_name":f"{name}" +"11"},headers={"Authori-zation":"Bearer " + str(login)})
        # 断言
        assert response.status_code == 200
        assert response.json()['msg'] == '提交成功'
        assert response.json()['code'] == '100017'
    @allure.story("删除用户分组")
    def test_delUserGroup(self,login):
        response = requests.delete(base_url+"/adminapi/user/user_group/del/"+str(id_list[0]),headers={"Authori-zation":"Bearer " + str(login)})
        assert response.status_code == 200
        assert response.json()['status'] == 200
        assert response.json()['msg'] == '删除成功!'

    @allure.story("添加话术")
    def test_huashu(self,login):
        response = requests.post(base_url+"/adminapi/app/wechat/speechcraft",json={"cate_id":"","title":"tester","message":f"{name}","sort":0},headers={"Authori-zation":"Bearer " + str(login)})
        # 断言状态码
        assert response.status_code == 200      # 协议响应状态码
        assert response.json()['msg'] == '创建话术成功'
        assert response.json()['status'] == 200     # 响应体里提取的，开发是可以自定义的
        assert response.json()['code'] == '400270'
        pass
    @allure.story("修改话术")
    def test_update(self,login):
        # 查询id
        response = requests.get(base_url+"/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=",headers={"Authori-zation":"Bearer " + str(login)})
        # 拿到id
        for i in response.json()['data']['list']:
            if i['message'] == str(name):
                id_list.append(i['id'])
        # 发起修改请求
        response = requests.put(base_url+"/adminapi/app/wechat/speechcraft/"+str(id_list[1]),headers={"Authori-zation":"Bearer " + str(login)},json={"cate_id":0,"title":"tester","message":f"{name}111","sort":0})
        # 断言
        assert response.status_code == 200
        assert response.json()['msg'] == '修改成功'
        assert response.json()['code'] == '100001'
        assert response.json()['status'] == 200
    @allure.story("删除话术")
    def test_delete(self,login):
        #
        response = requests.delete(base_url+"/adminapi/app/wechat/speechcraft/"+str(id_list[1]),headers={"Authori-zation":"Bearer " + str(login)})
        assert response.status_code == 200
        assert response.json()['msg'] == '删除成功'
        assert response.json()['code'] == '100002'
        assert response.json()['status'] ==200




"""
报告生成：
pytest [测试文件] --alluredir=allure
allure generate ./allure/ -o ./reports --clean
"""

